package com.dudu.dao;

import java.sql.Connection;
import java.util.List;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import com.dudu.bean.RemindInfo;

import core.CoreDao;
import core.db.DBConnectionUtil;

public class RemindDao {
    public int updateCodeTable(CoreDao dao, String code, String state) {
	Connection con = null;
	int result = -1;
	try {
	    con = DBConnectionUtil.getDBConnection("emate1");
	    StringBuffer sql = new StringBuffer("update sensitive_code_info set state=?");
	    sql.append("where code=?");
	    List<JSONObject> list = dao.serviceJsonSql(con, sql.toString(), new Object[] { state, code });
	    JSONObject obj = list.get(0);
	    result = obj.optInt("result");

	} catch (Exception ex) {
	    ex.printStackTrace();
	} finally {
	    DBConnectionUtil.close(null, null, con);
	}
	return result;
    }
    public int deleteCodeTable(CoreDao dao, String code) {
	Connection con = null;
	int result = -1;
	try {
	    con = DBConnectionUtil.getDBConnection("emate1");
	    StringBuffer sql = new StringBuffer("delete from sensitive_code_info where code=?");
	    List<JSONObject> list = dao.serviceJsonSql(con, sql.toString(), new Object[] {code });
	    JSONObject obj = list.get(0);
	    result = obj.optInt("result");

	} catch (Exception ex) {
	    ex.printStackTrace();
	} finally {
	    DBConnectionUtil.close(null, null, con);
	}
	return result;
    }
    public int getTotalAcount(CoreDao dao, String paramSql) {
	if (StringUtils.isNotBlank(paramSql)) {
	    paramSql = " and " + paramSql;
	}
	Connection con = null;
	int result = -1;
	try {
	    con = DBConnectionUtil.getDBConnection("emate1");
	    StringBuffer sql = new StringBuffer(" select count(1) as totalAcount " + " from sensitive_code_info where state=0 " + paramSql);

	    List<JSONObject> list = dao.serviceJsonSql(con, sql.toString(), new Object[] {});

	    JSONObject obj = list.get(0);
	    result = (Integer) obj.get("totalAcount");

	} catch (Exception ex) {
	    ex.printStackTrace();
	} finally {
	    DBConnectionUtil.close(null, null, con);
	}
	return result;
    }

    public List<RemindInfo> queryAll(CoreDao dao, String paramSql, String pageSql) {

	Connection con = null;
	List<RemindInfo> result = null;
	if (StringUtils.isNotBlank(paramSql)) {
	    paramSql = " and " + paramSql;
	}
	try {
	    con = DBConnectionUtil.getDBConnection("emate1");
	    StringBuffer sql = new StringBuffer(" select * from sensitive_code_info where state=0 " + paramSql + " order by code desc " + pageSql);
	    List<JSONObject> list = dao.serviceJsonSql(con, sql.toString(), new Object[] {});

	    if (list != null && list.size() > 0) {
		result = dao.covertToBean(list, RemindInfo.class);
	    }

	} catch (Exception ex) {
	    ex.printStackTrace();
	} finally {
	    DBConnectionUtil.close(null, null, con);
	}
	return result;
    }
}
